package com.situ.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.situ.mall.pojo.entity.Shipping;
import com.situ.mall.mapper.ShippingMapper;
import com.situ.mall.service.IShippingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Gao
 * @since 2025-10-15
 */
@Service
public class ShippingServiceImpl extends ServiceImpl<ShippingMapper, Shipping> implements IShippingService {
    @Autowired
    private ShippingMapper shippingMapper;

    @Override
    public List<Shipping> selectByUserId(Integer userId) {
        QueryWrapper<Shipping> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<Shipping> list = shippingMapper.selectList(wrapper);
        return list;
    }

    @Override
    public void updateStatus(Shipping shipping) {
        //首先将这个用户的所有地址设置为非默认
        UpdateWrapper<Shipping> wrapper = new UpdateWrapper<>();
        wrapper.set("status", 0);
        wrapper.eq("user_id", shipping.getUserId());
        shippingMapper.update(shipping, wrapper);

        //再将选中的设置为默认
        shippingMapper.updateById(shipping);
    }
}
